package cn.iocoder.yudao.module.dt.service.activitywrite;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.dt.controller.admin.activitywrite.vo.*;
import cn.iocoder.yudao.module.dt.dal.dataobject.activitywrite.ActivityWriteDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.dt.convert.activitywrite.ActivityWriteConvert;
import cn.iocoder.yudao.module.dt.dal.mysql.activitywrite.ActivityWriteMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dt.enums.ErrorCodeConstants.*;

/**
 * 活动核销记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ActivityWriteServiceImpl implements ActivityWriteService {

    @Resource
    private ActivityWriteMapper activityWriteMapper;

    @Override
    public Integer createActivityWrite(ActivityWriteCreateReqVO createReqVO) {
        // 插入
        ActivityWriteDO activityWrite = ActivityWriteConvert.INSTANCE.convert(createReqVO);
        activityWriteMapper.insert(activityWrite);
        // 返回
        return activityWrite.getId();
    }

    @Override
    public void updateActivityWrite(ActivityWriteUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateActivityWriteExists(updateReqVO.getId());
        // 更新
        ActivityWriteDO updateObj = ActivityWriteConvert.INSTANCE.convert(updateReqVO);
        activityWriteMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivityWrite(Integer id) {
        // 校验存在
        this.validateActivityWriteExists(id);
        // 删除
        activityWriteMapper.deleteById(id);
    }

    private void validateActivityWriteExists(Integer id) {
        if (activityWriteMapper.selectById(id) == null) {
            throw exception(ACTIVITY_WRITE_NOT_EXISTS);
        }
    }

    @Override
    public ActivityWriteDO getActivityWrite(Integer id) {
        return activityWriteMapper.selectById(id);
    }

    @Override
    public List<ActivityWriteDO> getActivityWriteList(Collection<Integer> ids) {
        return activityWriteMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ActivityWriteDO> getActivityWritePage(ActivityWritePageReqVO pageReqVO) {
        return activityWriteMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ActivityWriteDO> getActivityWriteList(ActivityWriteExportReqVO exportReqVO) {
        return activityWriteMapper.selectList(exportReqVO);
    }

}
